package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.OutOrderInfoBean;
import com.project.bean.ProductBean;
import com.project.bean.SellCarBean;
import com.project.bean.SellOrderBean;
import com.project.mapper.IOutOrderInfoMapper;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISellCarMapper;
import com.project.mapper.ISellOrderMapper;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售车实现类
 */
@Service
@Transactional
public class SellCarServiceImpl implements ISellCarService {
    @Autowired
    private ISellCarMapper sellCarMapper;
    @Autowired
    private ISellOrderMapper sellOrderMapper;
    @Autowired
    private IOutOrderInfoMapper outOrderInfoMapper;
    @Autowired
    private IProductMapper productMapper;

    @Override
    public List<SellCarBean> findSellcarByloginUser(String userName) {
        return sellCarMapper.findSellcarByloginUser(userName);
    }

    @Override
    public void addSellCarShop(Integer productId,String userName) {
        QueryWrapper<SellCarBean> qw=new QueryWrapper<>();
        qw.eq("fk_productId",productId);
        qw.eq("s_userName",userName);
          SellCarBean sellCarBean=sellCarMapper.selectOne(qw);
          if (sellCarBean !=null){//该商品在销售车中存在,数量加一
              sellCarBean.setOutNum(sellCarBean.getOutNum()+1);
              sellCarMapper.updateById(sellCarBean);
          }else {//该商品在销售车中不存在,新增销售车商品
              sellCarMapper.insert(new SellCarBean(productId,1,userName));
          }
    }


    @Override
    public void delShop(Integer sellCarId) {
            sellCarMapper.deleteById(sellCarId);
    }

    @Override
    public void updateSellcarNum(Integer sellCarId, Integer newNum) {
          SellCarBean sellCarObj= sellCarMapper.selectById(sellCarId);
          sellCarObj.setOutNum(newNum);
          sellCarMapper.updateById(sellCarObj);
    }

    @Override
    public void removeStock(SellOrderBean sellOrder,String userName) {
        //添加销售基本信息
           sellOrderMapper.insert(sellOrder);
           //根据销售员,查询销售车信息集合
           List<SellCarBean> list=sellCarMapper.findSellcarByloginUser(userName);
        for (SellCarBean sellCar: list) {
            //遍历销售车集合，循环添加销售明细
            OutOrderInfoBean outOrderInfo=new OutOrderInfoBean();
            outOrderInfo.setSellId(sellOrder.getSellId());
            outOrderInfo.setProductId(sellCar.getProductId());
            outOrderInfo.setNum(sellCar.getOutNum());
            outOrderInfo.setPrice(sellCar.getProduct().getSellPrice());
            outOrderInfoMapper.insert(outOrderInfo);
            //循环修改库存数量（类减）
           ProductBean product=productMapper.selectById(sellCar.getProductId());
            product.setStock(product.getStock() - sellCar.getOutNum());
            productMapper.updateById(product);
        }
        //根据销售员清空销售车
        QueryWrapper<SellCarBean> qw=new QueryWrapper<>();
        qw.eq("s_userName",userName);
          sellCarMapper.delete(qw);
    }
}
